package cc.rengu.igas.channel.wechat.core.service.trans;

import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatServiceConstant;
import cc.rengu.igas.channel.wechat.common.constant.WechatTreeNodeConstant;
import cc.rengu.igas.channel.wechat.common.dao.TemplateCfgMapper;
import cc.rengu.igas.channel.wechat.common.dao.impl.TemplateCfgMapperImpl;
import cc.rengu.igas.channel.wechat.common.entity.TemplateCfg;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.common.enums.TemplateTypeEnum;
import cc.rengu.igas.channel.wechat.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.wechat.core.service.base.MsgPushService;
import cc.rengu.igas.channel.wechat.facade.request.UnifiedMsgPushRequest;
import cc.rengu.igas.channel.wechat.facade.response.UnifiedMsgPushResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 统一消息推送
 */
public class UnifiedMsgPushService extends MsgPushService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        UnifiedMsgPushRequest unifiedMsgPushRequest = new UnifiedMsgPushRequest();
        ConvertUtil.convertOutput(unifiedMsgPushRequest);
        return unifiedMsgPushRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        UnifiedMsgPushResponse unifiedMsgPushResponse = new UnifiedMsgPushResponse();
        UnifiedMsgPushRequest unifiedMsgPushRequest = (UnifiedMsgPushRequest) request;
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(WechatTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);

        /*从redis缓存获取accessToken*/
        String accessToken = RedisUtil.onceGet(WechatParamConstant.ACCESS_TOKEN_CACHE_NAME + dstChannelCfg.getSecPlanId());
        if (accessToken == null) {
            accessToken = getAccessToken(dstChannelCfg);
        }
        TemplateCfgMapper templateCfgMapper = new TemplateCfgMapperImpl();
        TemplateCfg templateCfg = templateCfgMapper.selectTemplateCfgByInstIdAndType(unifiedMsgPushRequest.getHeader().getInstId(),
                unifiedMsgPushRequest.getTemplateId());
        if (templateCfg == null) {
            throw new BizException(RespCodeEnum.TEMPLATE_CFG_NOT_EXIST.getRespCode(), RespCodeEnum.TEMPLATE_CFG_NOT_EXIST.getRespDesc());
        }

        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(unifiedMsgPushRequest.getHeader().getInstId(),
                WechatParamConstant.WECHAT_MSG_PUSH_FIRSTDATA_PARAM, TemplateTypeEnum.MSG06.getTypeCode());
        if (sysParam == null) {
            rglog.error("获取微信消息推送firstdata失败，请配置");
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }

        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.ACCESS_TOKEN, accessToken);
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.TEMPLATE_ID, templateCfg.getTemplateId());
        xmlTreeUtil.setXmlTreeStringValue(WechatTreeNodeConstant.JUMP_URL, templateCfg.getTemplateUrl());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, unifiedMsgPushRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(WechatServiceConstant.WECHAT_PUBLIC_CLIENT_SERVICE, 0);
        if (xmlTreeUtil.isExist(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE)) {
            xmlTreeUtil.deleteXmlTreeNode(WechatTreeNodeConstant.DEFINE_HTTP_URL_NODE);
        }
        if (0 != iReturnCode) {
            rglog.error("调用微信消息推送服务失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        unifiedMsgPushResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        unifiedMsgPushResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(unifiedMsgPushResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        UnifiedMsgPushResponse unifiedMsgPushResponse = (UnifiedMsgPushResponse) response.getResult();
        ConvertUtil.convertInput(unifiedMsgPushResponse);
    }
}
